package com.sgcc.operate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sgcc.common.security.service.TokenService;
import com.sgcc.operate.domain.OIndiContent;
import com.sgcc.operate.domain.OIndiDeco;
import com.sgcc.operate.mapper.OIndiContentMapper;
import com.sgcc.operate.mapper.OIndiDecoMapper;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.operate.mapper.OIndiFormMapper;
import com.sgcc.operate.domain.OIndiForm;
import com.sgcc.operate.service.IOIndiFormService;

/**
 * 运营指标制定Service业务层处理
 *
 * @author sgcc
 * @date 2021-03-23
 */
@Service
public class OIndiFormServiceImpl implements IOIndiFormService {
    @Autowired
    private OIndiFormMapper oIndiFormMapper;
    @Autowired
    private OIndiContentMapper oIndiContentMapper;
    @Autowired
    private OIndiDecoMapper oIndiDecoMapper;
    @Autowired
    private RemoteDictDataService remoteDictDataService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询运营指标制定
     *
     * @param id 运营指标制定ID
     * @return 运营指标制定
     */
    @Override
    public OIndiForm selectOIndiFormById(String id) {
        OIndiForm oIndiForm = oIndiFormMapper.selectOIndiFormById(id);
        OIndiDeco oIndiDeco = new OIndiDeco();
        oIndiDeco.setIndiformid(oIndiForm.getId());
        oIndiForm.setTimeSlotList(oIndiDecoMapper.selectOIndiDecoList(oIndiDeco));
        return oIndiForm;
    }

    /**
     * 查询运营指标制定列表
     *
     * @param oIndiForm 运营指标制定
     * @return 运营指标制定
     */
    @Override
    public List<OIndiForm> selectOIndiFormList(OIndiForm oIndiForm) {
        return oIndiFormMapper.selectOIndiFormList(oIndiForm);
    }

    /**
     * 新增运营指标制定
     *
     * @param oIndiForm 运营指标制定
     * @return 结果
     */
    @Override
    public int insertOIndiForm(OIndiForm oIndiForm) {
        LoginUser loginUser = tokenService.getLoginUser();
        oIndiForm.setCreateDate(new Date());
        oIndiForm.setUpdateDate(new Date());
        oIndiForm.setCreateBy(loginUser.getUserid());
        oIndiForm.setUpdateBy(loginUser.getUserid());
        oIndiForm.setModifyorg(loginUser.getSysUser().getDeptId());
        oIndiForm.setDelFlag("0");
        oIndiForm.setModifytime(new Date());
        oIndiForm.setModifyemp(loginUser.getUserid());
        oIndiForm.setManageorg(oIndiForm.getModifyorg());
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        oIndiForm.setIndistatus("01");
        oIndiForm.setId(id);
        int rows = oIndiFormMapper.insertOIndiForm(oIndiForm);
        if (rows == 0) {
            return 0;
        }
        if (oIndiForm.getOindiDecoList() != null) {
            //删除原来运营指标分解的数据
            OIndiDeco OIndiDeco = new OIndiDeco();
            OIndiDeco.setIndiformid(oIndiForm.getId());
            List<OIndiDeco> list = oIndiDecoMapper.selectOIndiDecoList(OIndiDeco);
            if (list != null) {
                for (OIndiDeco oIndiDeco : list) {
                    oIndiDecoMapper.deleteOIndiDecoById(oIndiDeco.getId());
                }
            }
            for (OIndiDeco oIndiDeco : oIndiForm.getTimeSlotList()) {
                oIndiDeco.setIndiformid(oIndiForm.getId());
                String decoid = UUID.randomUUID().toString().replaceAll("-", "");
                oIndiDeco.setId(decoid);
                oIndiDeco.setCreateDate(new Date());
                oIndiDeco.setUpdateDate(new Date());
                oIndiDeco.setCreateBy(loginUser.getUserid());
                oIndiDeco.setUpdateBy(loginUser.getUserid());
                oIndiDeco.setDelFlag("0");
                oIndiDecoMapper.insertOIndiDeco(oIndiDeco);
            }
        }
        return 1;
    }

    /**
     * 修改运营指标制定
     *
     * @param oIndiForm 运营指标制定
     * @return 结果
     */
    @Override
    public int updateOIndiForm(OIndiForm oIndiForm) {
        LoginUser loginUser = tokenService.getLoginUser();
        oIndiForm.setUpdateDate(new Date());
        oIndiForm.setUpdateBy(loginUser.getUserid());
        int rows = oIndiFormMapper.updateOIndiForm(oIndiForm);

        if (rows == 0) {
            return 0;
        }

        if (oIndiForm.getOindiDecoList() != null) {
            //删除原来运营指标分解的数据
            OIndiDeco OIndiDeco = new OIndiDeco();
            OIndiDeco.setIndiformid(oIndiForm.getId());
            List<OIndiDeco> list = oIndiDecoMapper.selectOIndiDecoList(OIndiDeco);
            if (list != null) {
                for (OIndiDeco oIndiDeco : list) {
                    oIndiDecoMapper.deleteOIndiDecoById(oIndiDeco.getId());
                }
            }
            for (OIndiDeco oIndiDeco : oIndiForm.getTimeSlotList()) {
                oIndiDeco.setIndiformid(oIndiForm.getId());
                String decoid = UUID.randomUUID().toString().replaceAll("-", "");
                oIndiDeco.setId(decoid);
                oIndiDeco.setCreateDate(new Date());
                oIndiDeco.setUpdateDate(new Date());
                oIndiDeco.setCreateBy(loginUser.getUserid());
                oIndiDeco.setUpdateBy(loginUser.getUserid());
                oIndiDeco.setDelFlag("0");
                oIndiDecoMapper.insertOIndiDeco(oIndiDeco);
            }
        }
        return 1;
    }

    /**
     * 批量删除运营指标制定
     *
     * @param ids 需要删除的运营指标制定ID
     * @return 结果
     */
    @Override
    public int deleteOIndiFormByIds(String[] ids) {
        oIndiFormMapper.deleteByLogicByIds(ids);
        oIndiDecoMapper.deleteByLogicByindiformIds(ids);
        return 1;
    }


    /**
     * 删除运营指标制定信息
     *
     * @param id 运营指标制定ID
     * @return 结果
     */
    @Override
    public int deleteOIndiFormById(String id) {
        return oIndiFormMapper.deleteOIndiFormById(id);
    }

    @Override
    public List<OIndiContent> getOIndiContent(String id) {
        OIndiContent oIndiContent = new OIndiContent();
        oIndiContent.setIndiid(id);
        return oIndiContentMapper.selectOIndiContentList(oIndiContent);
    }
}
